{
 "cells": [
  {
   "cell_type": "markdown",
   "source": [
    "# 导入包"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "outputs": [],
   "source": [
    "from tqdm import tqdm\n",
    "import os\n",
    "from typing import List\n",
    "import numpy as np\n",
    "import cv2\n",
    "from PIL import Image, ImageDraw, ImageFont\n",
    "from xml.dom.minidom import parse\n",
    "import math\n",
    "from pathlib import Path\n",
    "import shutil\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "# 常用函数"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "outputs": [],
   "source": [
    "# 批量将两张图片水平拼接在一起\n",
    "def batch_merge_img(img1_dir,img2_dir,merge_dir):\n",
    "    for filename in tqdm(os.listdir(img2_dir)):\n",
    "        img1_path = os.path.join(img1_dir,filename)\n",
    "        img2_path = os.path.join(img2_dir,filename)\n",
    "        merged_path = os.path.join(merge_dir,filename)\n",
    "        img1 = np.array(Image.open(img1_path))\n",
    "        img2 = np.array(Image.open(img2_path))\n",
    "        img_merged = np.hstack([img1,img2])\n",
    "        Image.fromarray(img_merged).save(merged_path)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "# 读取xml标签文件，并转换格式和生成标注图片\n",
    "def get_xml_data(xml_path, img_path, annotated_img_path, save_txt_path,dict_color, size=1024):\n",
    "    dom = parse(xml_path)\n",
    "    root = dom.documentElement\n",
    "    objects = root.getElementsByTagName(\"object\")\n",
    "    f = open(save_txt_path, 'w')\n",
    "    img = Image.open(img_path)\n",
    "    draw = ImageDraw.Draw(img)\n",
    "    for box in objects:\n",
    "        cls_name = box.getElementsByTagName(\"name\")[0].childNodes[0].data\n",
    "        x0 = float(box.getElementsByTagName(\"x0\")[0].childNodes[0].data)\n",
    "        y0 = float(box.getElementsByTagName(\"y0\")[0].childNodes[0].data)\n",
    "        x1 = float(box.getElementsByTagName(\"x1\")[0].childNodes[0].data)\n",
    "        y1 = float(box.getElementsByTagName(\"y1\")[0].childNodes[0].data)\n",
    "        x2 = float(box.getElementsByTagName(\"x2\")[0].childNodes[0].data)\n",
    "        y2 = float(box.getElementsByTagName(\"y2\")[0].childNodes[0].data)\n",
    "        x3 = float(box.getElementsByTagName(\"x3\")[0].childNodes[0].data)\n",
    "        y3 = float(box.getElementsByTagName(\"y3\")[0].childNodes[0].data)\n",
    "        draw.line([(x0, y0), (x1, y1), (x2, y2), (x3, y3), (x0, y0)], fill=dict_color.get(cls_name), width=3)\n",
    "        line = trans([cls_name, x0, y0, x1, y1, x2, y2, x3, y3], size)\n",
    "        if line:\n",
    "            f.write(line)\n",
    "    f.close()\n",
    "    img.save(annotated_img_path)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "outputs": [],
   "source": [
    "# 根据xml结果给图片打上标注\n",
    "def anno_from_xml(xml_path,img_path,anno_path,dict_color,size=1024):\n",
    "    dom = parse(xml_path)\n",
    "    root = dom.documentElement\n",
    "    objects = root.getElementsByTagName(\"object\")\n",
    "    img = Image.open(img_path)\n",
    "    draw = ImageDraw.Draw(img)\n",
    "    for box in objects:\n",
    "        cls_name = box.getElementsByTagName(\"name\")[0].childNodes[0].data\n",
    "        points = box.getElementsByTagName(\"point\")\n",
    "        point0 = tuple(map(int,points[0].childNodes[0].data.split(',')))\n",
    "        point1 = tuple(map(int,points[1].childNodes[0].data.split(',')))\n",
    "        point2 = tuple(map(int,points[2].childNodes[0].data.split(',')))\n",
    "        point3 = tuple(map(int,points[3].childNodes[0].data.split(',')))\n",
    "        draw.line([point0,point1,point2,point3,point0], fill=dict_color.get(cls_name), width=3)\n",
    "\n",
    "    img.save(anno_path)\n",
    "    "
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "anno_from_xml(\n",
    "    r'/home/xs/dataset/object_detection/test/ship/result_xml/011781.xml',\n",
    "    r'/home/xs/dataset/object_detection/test/ship/images/011781.jpg',\n",
    "    r'/home/xs/dataset/object_detection/test/ship/011781.jpg',\n",
    "    {'ship': 'red'}\n",
    ")"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "# 标签格式转换函数\n",
    "def trans(box: List, dict_class,size=1024):\n",
    "    cls_name = box[0]\n",
    "    data = np.array(box[1:])\n",
    "    data = data.reshape(4, 2).astype(int)  # 这里必须是整数\n",
    "    rect = cv2.minAreaRect(data)  # 得到最小外接矩形的（中心(x,y), (宽,高), 旋转角度）\n",
    "    c_x = rect[0][0]\n",
    "    c_y = rect[0][1]\n",
    "    w = rect[1][0]\n",
    "    h = rect[1][1]\n",
    "    new_box = np.array([c_x, c_y, w, h])\n",
    "    if (new_box < 0).any() or (new_box > size).any():  # 坐标为负数，说明在边界处，或者box宽度超过图片尺寸，直接跳过\n",
    "        return None\n",
    "    theta = int(rect[-1])\n",
    "    long_side, short_side = w, h\n",
    "    if w < h:\n",
    "        long_side = h\n",
    "        short_side = w\n",
    "        theta -= 90\n",
    "    theta += 90\n",
    "    line = f'{dict_class.get(cls_name, 2)} {c_x / size} {c_y / size} {long_side / size} {short_side / size} {theta}\\n'\n",
    "    return line"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "# 图片统一尺寸，宽高相同，补pdding\n",
    "def uniform_img_size(img_array, size):\n",
    "    \"\"\"\n",
    "    将训练图片统一尺寸\n",
    "    保持长宽比不变，周围填充0以满足输出的大小要求\n",
    "    :param img_array:\n",
    "    :param size: 图片大小，宽高一样，1024\n",
    "    :return:\n",
    "    \"\"\"\n",
    "    img_w, img_h = img_array.shape[1], img_array.shape[0]\n",
    "    scale = size / max(img_h, img_w)\n",
    "    new_w = int(img_w * scale)\n",
    "    new_h = int(img_h * scale)\n",
    "    resized_image = cv2.resize(img_array, (new_w, new_h), interpolation=cv2.INTER_CUBIC)\n",
    "    canvas = np.full((size, size, 3), 0).astype(np.uint8)\n",
    "    canvas[(size - new_h) // 2:(size - new_h) // 2 + new_h, (size - new_w) // 2:(size - new_w) // 2 + new_w,\n",
    "    :] = resized_image\n",
    "    return canvas, scale, new_w, new_h"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "# 读取txt标签，并转换成yolov5格式\n",
    "def get_txt_data(txt_path, img_path, new_img_path, annotated_img_path, save_txt_path,dict_color, size):\n",
    "    \"\"\"\n",
    "    将类似 x1 y1 x2 y2 x3 y3 x4 y4 classname id 这种格式的txt标注文件转换成yolov5旋转框需要的格式:\n",
    "    id center_x center_y long_side short_side angle\n",
    "    坐标进行归一化处理\n",
    "    将原始训练图片统一大小，并生成标注图片\n",
    "    :param txt_path:\n",
    "    :param img_path:\n",
    "    :param annotated_img_path:\n",
    "    :param save_txt_path:\n",
    "    :param size:\n",
    "    :return:\n",
    "    \"\"\"\n",
    "    img_array = np.array(Image.open(img_path))\n",
    "    img_resized_array, scale, new_w, new_h = uniform_img_size(img_array, size)\n",
    "    img_resized = Image.fromarray(img_resized_array)\n",
    "    img_resized.save(new_img_path)\n",
    "    draw = ImageDraw.Draw(img_resized)\n",
    "    f = open(txt_path)\n",
    "    f2 = open(save_txt_path, 'w')\n",
    "    data = f.readlines()\n",
    "    for line in data:\n",
    "        box = line.strip().split()\n",
    "        cls_name = box[-2]\n",
    "        cls_id = box[-1]\n",
    "        box = list(map(float, box[:8]))\n",
    "        xs = np.array(box[::2]) * scale + (size - new_w) // 2\n",
    "        ys = np.array(box[1::2]) * scale + (size - new_h) // 2\n",
    "        draw.line([(xs[0], ys[0]), (xs[1], ys[1]), (xs[2], ys[2]), (xs[3], ys[3]), (xs[0], ys[0])],\n",
    "                  fill=dict_color.get(cls_name), width=3)\n",
    "        line = trans([cls_id, xs[0], ys[0], xs[1], ys[1], xs[2], ys[2], xs[3], ys[3]], size)\n",
    "        if line:\n",
    "            f2.write(line)\n",
    "    f.close()\n",
    "    f2.close()\n",
    "    img_resized.save(annotated_img_path)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "outputs": [],
   "source": [
    "# 判断某个txt标签文件是否合格\n",
    "def label_is_valid(label_path,class_ids):\n",
    "    with open(label_path) as f:\n",
    "        data = f.readlines()\n",
    "        for line in data:\n",
    "            data = line.split()\n",
    "            class_id,angle = int(data[0]),int(data[-1])\n",
    "            points = np.array(list(map(float,data[1:-1])))\n",
    "            if class_id not in class_ids or (points>1).any() or\\\n",
    "                (points<0).any() or angle<0 or angle>180 or \\\n",
    "                    len(data) !=6 or points[2]<points[3]:\n",
    "                return False\n",
    "    return True"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "# 从单个txt标签文件中提取某个类别，并写入到新的txt标签文件中\n",
    "def get_single_class_from_txt(txt_path,new_txt_path,class_id):\n",
    "    \"\"\"\n",
    "    :param txt_path: 标签文本路径\n",
    "    :param new_txt_path: 新标签文本路径\n",
    "    :param class_id: 要提取的类别id\n",
    "    :return:\n",
    "    \"\"\"\n",
    "    with open(txt_path) as f1, open(new_txt_path,'w') as f2:\n",
    "        for line in f1:\n",
    "            if line[0] == str(class_id):\n",
    "                f2.write('0'+line[1:])"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "outputs": [],
   "source": [
    "# 删除空白文件\n",
    "def delete_blank_file(file_path):\n",
    "    with open(file_path) as f:\n",
    "            data = f.read()\n",
    "    if not data:\n",
    "        os.remove(file_path)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "# 读取xml标签，统一图片尺寸，生成标注图片，生成txt标签文件\n",
    "def xml2annotation_txt(xml_path, img_path,new_img_path, annotated_img_path, save_txt_path, size=1024):\n",
    "    dom = parse(str(xml_path))\n",
    "    root = dom.documentElement\n",
    "    objects = root.getElementsByTagName(\"HRSC_Object\")\n",
    "    f = open(save_txt_path, 'w')\n",
    "    img_array = np.array(Image.open(img_path))\n",
    "    img,scale,w_resized,h_resized=uniform_img_size(img_array,size)\n",
    "    img = Image.fromarray(img)\n",
    "    img.save(new_img_path)\n",
    "    draw = ImageDraw.Draw(img)\n",
    "    for box in objects:\n",
    "        cx = float(box.getElementsByTagName(\"mbox_cx\")[0].childNodes[0].data)\n",
    "        cy = float(box.getElementsByTagName(\"mbox_cy\")[0].childNodes[0].data)\n",
    "        w = float(box.getElementsByTagName(\"mbox_w\")[0].childNodes[0].data)\n",
    "        h = float(box.getElementsByTagName(\"mbox_h\")[0].childNodes[0].data)\n",
    "        angle_pi = float(box.getElementsByTagName(\"mbox_ang\")[0].childNodes[0].data)\n",
    "        new_cx = cx * scale + (size - w_resized) // 2\n",
    "        new_cy = cy * scale + (size - h_resized) // 2\n",
    "        new_w,new_h = w * scale, h * scale\n",
    "        points,angle = get_box_points([new_cx,new_cy,new_w,new_h,angle_pi])\n",
    "        draw.polygon(points,outline='red')\n",
    "        font = ImageFont.truetype(\"consola.ttf\", 30, encoding=\"unic\"  )  # 设置字体\n",
    "        draw.text((new_cx,new_cy), f'{angle}', 'fuchsia', font)\n",
    "        f.write(f'{0} {new_cx/size} {new_cy/size} {max(new_w,new_h)/size} {min(new_w,new_h)/size} {angle}\\n')\n",
    "    f.close()\n",
    "    img.save(annotated_img_path)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "outputs": [],
   "source": [
    "# 根据[x,y,w,h,angle]求矩形框四个顶点的坐标\n",
    "def get_box_points(result):\n",
    "    result=np.array(result)\n",
    "    x=result[0]\n",
    "    y=result[1]\n",
    "    width=result[2]\n",
    "    height=result[3]\n",
    "    # height=result[2]\n",
    "    # width=result[3]\n",
    "    angle=result[4]\n",
    "    anglePi = angle * math.pi/180\n",
    "\n",
    "    cosA = math.cos(anglePi)\n",
    "    sinA = math.sin(anglePi)\n",
    "\n",
    "    x1=x-0.5*width\n",
    "    y1=y-0.5*height\n",
    "\n",
    "    x0=x+0.5*width\n",
    "    y0=y1\n",
    "\n",
    "    x2=x1\n",
    "    y2=y+0.5*height\n",
    "\n",
    "    x3=x0\n",
    "    y3=y2\n",
    "\n",
    "    x0n= (x0 -x)*cosA -(y0 - y)*sinA + x\n",
    "    y0n = (x0-x)*sinA + (y0 - y)*cosA + y\n",
    "\n",
    "    x1n= (x1 -x)*cosA -(y1 - y)*sinA + x\n",
    "    y1n = (x1-x)*sinA + (y1 - y)*cosA + y\n",
    "\n",
    "    x2n= (x2 -x)*cosA -(y2 - y)*sinA + x\n",
    "    y2n = (x2-x)*sinA + (y2 - y)*cosA + y\n",
    "\n",
    "    x3n= (x3 -x)*cosA -(y3 - y)*sinA + x\n",
    "    y3n = (x3-x)*sinA + (y3 - y)*cosA + y\n",
    "    return [x0n,y0n,x1n,y1n,x2n,y2n,x3n,y3n]"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "# 合并两种标签\n",
    "def merge_xml_txt(xml_path, img_path,annotated_img_path, save_txt_path,dict_color,dict_class, size=1024):\n",
    "    dom = parse(str(xml_path))\n",
    "    root = dom.documentElement\n",
    "    objects = root.getElementsByTagName(\"object\")\n",
    "    f = open(save_txt_path, 'r+')\n",
    "    img = Image.open(img_path)\n",
    "    draw = ImageDraw.Draw(img)\n",
    "    for line in f:\n",
    "        box = line.split()\n",
    "        cx,cy,h,w = map(lambda val:float(val)*size,box[1:-1])\n",
    "        angle_pi = float(box[-1])*math.pi/180\n",
    "        points,angle = get_box_points([cx,cy,w,h,angle_pi])\n",
    "        draw.polygon(points,outline='red')\n",
    "        font = ImageFont.truetype(\"consola.ttf\", 30, encoding=\"unic\"  )  # 设置字体\n",
    "        draw.text((cx,cy), f'{angle}', 'fuchsia', font)\n",
    "\n",
    "    for box in objects:\n",
    "        cls_name = box.getElementsByTagName(\"name\")[0].childNodes[0].data\n",
    "        cx = float(box.getElementsByTagName(\"cx\")[0].childNodes[0].data)\n",
    "        cy = float(box.getElementsByTagName(\"cy\")[0].childNodes[0].data)\n",
    "        w = float(box.getElementsByTagName(\"w\")[0].childNodes[0].data)\n",
    "        h = float(box.getElementsByTagName(\"h\")[0].childNodes[0].data)\n",
    "        angle_pi = float(box.getElementsByTagName(\"angle\")[0].childNodes[0].data)\n",
    "        points,angle = get_box_points([cx,cy,w,h,angle_pi])\n",
    "        draw.polygon(points,outline=dict_color.get(cls_name,'blue'))\n",
    "        font = ImageFont.truetype(\"consola.ttf\", 30, encoding=\"unic\"  )  # 设置字体\n",
    "        draw.text((cx,cy), f'{angle}', 'fuchsia', font)\n",
    "        cls_id = dict_class.get(cls_name,2)\n",
    "        f.write(f'{cls_id} {cx/size} {cy/size} {max(w,h)/size} {min(w,h)/size} {angle}\\n')\n",
    "    f.close()\n",
    "    img.save(annotated_img_path)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "outputs": [],
   "source": [
    "# 根据txt标签文件，生成标注图片\n",
    "def annotate_image(label_path,image_path,annotation_path,dict_color,size=1024):\n",
    "    img = Image.open(image_path)\n",
    "    draw = ImageDraw.Draw(img)\n",
    "    with open(label_path) as f:\n",
    "        for line in f:\n",
    "            data = line.split()\n",
    "            class_id,angle= int(data[0]),int(data[-1])\n",
    "            box = [float(xy)*size for xy in data[1:-1]]\n",
    "            box.append(angle)\n",
    "            points = get_box_points(box)\n",
    "            draw.polygon(points,outline=dict_color.get(class_id))\n",
    "            font = ImageFont.truetype(\"consola.ttf\", 30, encoding=\"unic\"  )  # 设置字体\n",
    "            draw.text((box[0],box[1]), f'{angle}', 'fuchsia', font)\n",
    "    img.save(annotation_path)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 104,
   "outputs": [],
   "source": [
    "# 给某个目录下的所有图片统一成宽高相同的size\n",
    "def batch_uniform_img_size(img_dir,img_resized_dir,size=800):\n",
    "    for file in tqdm(os.listdir(img_dir)):\n",
    "        img_path = os.path.join(img_dir,file)\n",
    "        img_array = cv2.imread(img_path)\n",
    "        img_resized_array,*_ = uniform_img_size(img_array,size)\n",
    "        img_resized_path = os.path.join(img_resized_dir,file)\n",
    "        cv2.imwrite(img_resized_path,img_resized_array)\n",
    "\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "# 处理dota数据集"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "outputs": [],
   "source": [
    "img_dir = r'F:\\dataset\\object_detection\\DOTA-v2.0\\train_data\\three-class\\images/train'\n",
    "annotated_images_dir = r'F:\\dataset\\object_detection\\DOTA-v2.0\\train_data\\three-class\\images\\train_annotation'\n",
    "os.makedirs(annotated_images_dir,exist_ok=True)\n",
    "xml_dir = r'F:\\dataset\\object_detection\\DOTA-v2.0\\train_data\\three-class\\labels\\train_xml'\n",
    "save_txt_dir = r'F:\\dataset\\object_detection\\DOTA-v2.0\\train_data\\three-class\\labels\\train'\n",
    "os.makedirs(save_txt_dir,exist_ok=True)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 找出所有不合格的txt标签文件"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|██████████| 5960/5960 [00:01<00:00, 3489.92it/s]\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "100001124.txt\n",
      "100001131.txt\n",
      "100001337.txt\n"
     ]
    }
   ],
   "source": [
    "labeltxt_dir = r'F:\\dataset\\object_detection\\DOTA-v2.0\\train_data\\ship\\labels\\train'\n",
    "for file in tqdm(os.listdir(labeltxt_dir)):\n",
    "    txt_path = os.path.join(labeltxt_dir,file)\n",
    "    if not label_is_valid(txt_path,[0,1,2]):\n",
    "        print(file)\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 将txt标签目录下的所有txt文件批量提取某个类别，并非生成新的标签文件到指定目录下"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 91,
   "outputs": [],
   "source": [
    "labeltxt_dir = r'F:\\dataset\\object_detection\\DOTA-v2.0\\train_data\\three-class\\labels\\val'\n",
    "new_labeltxt_dir = r'F:\\dataset\\object_detection\\DOTA-v2.0\\train_data\\plane\\labels\\val'\n",
    "os.makedirs(new_labeltxt_dir,exist_ok=True)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "class_id = 1\n",
    "for file in tqdm(os.listdir(labeltxt_dir)):\n",
    "    labeltxt_path = os.path.join(labeltxt_dir,file)\n",
    "    new_labeltxt_path = os.path.join(new_labeltxt_dir,file)\n",
    "    get_single_class_from_txt(labeltxt_path,new_labeltxt_path,class_id)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 删除空白标签txt文件"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 92,
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|██████████| 3526/3526 [00:00<00:00, 6019.76it/s]\n"
     ]
    }
   ],
   "source": [
    "for file in tqdm(os.listdir(new_labeltxt_dir)):\n",
    "    txt_path = os.path.join(new_labeltxt_dir,file)\n",
    "    delete_blank_file(txt_path)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 根据标签文件复制图片到指定目录"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 93,
   "outputs": [],
   "source": [
    "labeltxts_dir = r'F:\\dataset\\object_detection\\DOTA-v2.0\\train_data\\plane\\labels\\val'\n",
    "imgs_dir = r'F:\\dataset\\object_detection\\DOTA-v2.0\\train_data\\three-class\\images\\val'\n",
    "new_imgs_dir = r'F:\\dataset\\object_detection\\DOTA-v2.0\\train_data\\plane\\images\\val'\n",
    "os.makedirs(new_imgs_dir,exist_ok=True)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 94,
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|██████████| 1444/1444 [01:18<00:00, 18.35it/s]\n"
     ]
    }
   ],
   "source": [
    "for file in tqdm(os.listdir(labeltxts_dir)):\n",
    "    img_path = os.path.join(imgs_dir,file.replace('.txt','.png'))\n",
    "    shutil.copy(img_path,new_imgs_dir)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "## 发现不合格的标签文件"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|██████████| 4474/4474 [00:01<00:00, 3584.97it/s]\n"
     ]
    }
   ],
   "source": [
    "for file in tqdm(os.listdir(labeltxts_dir)):\n",
    "    labeltxt_path = os.path.join(labeltxts_dir,file)\n",
    "    if not label_is_valid(labeltxt_path,[0]):\n",
    "        print(file)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 生成标注图片"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "outputs": [],
   "source": [
    "img_dir = r'F:\\dataset\\object_detection\\DOTA-v2.0\\train_data\\bigship\\images\\train'\n",
    "label_dir = r'F:\\dataset\\object_detection\\DOTA-v2.0\\train_data\\bigship\\labels\\train_x'\n",
    "annotation_dir = r'F:\\dataset\\object_detection\\DOTA-v2.0\\train_data\\bigship\\images\\train_x_annotation'\n",
    "os.makedirs(annotation_dir,exist_ok=True)\n",
    "dict_color = {0:'red'}"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|██████████| 1055/1055 [05:10<00:00,  3.40it/s]\n"
     ]
    }
   ],
   "source": [
    "for file in tqdm(os.listdir(label_dir)):\n",
    "    label_path = os.path.join(label_dir,file)\n",
    "    image_path = os.path.join(img_dir,file.replace('.txt','.png'))\n",
    "    annotation_path = os.path.join(annotation_dir,file.replace('.txt','.png'))\n",
    "    annotate_image(label_path,image_path,annotation_path,dict_color)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 86,
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|██████████| 7082/7082 [00:00<00:00, 27520.55it/s]\n"
     ]
    }
   ],
   "source": [
    "for file in tqdm(os.listdir(annotation_dir)):\n",
    "    anno_path = os.path.join(annotation_dir,file)\n",
    "    img_path = os.path.join(img_dir,file)\n",
    "    if not os.path.exists(img_path):\n",
    "        os.remove(anno_path)\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 删除一部分小船样本，提高大船占比"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|██████████| 6970/6970 [00:00<00:00, 17507.35it/s]\n"
     ]
    }
   ],
   "source": [
    "list_imgnames = os.listdir(new_imgs_dir)\n",
    "nums = len(list_imgnames)\n",
    "count = 0\n",
    "for file in tqdm(list_imgnames):\n",
    "    img_path = os.path.join(new_imgs_dir,file)\n",
    "    label_path = os.path.join(labeltxts_dir,file.replace('.png','.txt'))\n",
    "    if file.startswith('P') and count % 6 == 0:\n",
    "        os.remove(img_path)\n",
    "        os.remove(label_path)\n",
    "    count +=1\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "# 处理HRSC2016大船数据"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "outputs": [],
   "source": [
    "xml_dir = Path(r'F:\\dataset\\object_detection\\HRSC2016_dataset\\labelxmls')\n",
    "img_dir = Path(r'F:\\dataset\\object_detection\\HRSC2016_dataset\\images')\n",
    "new_img_dir = Path(r'F:\\dataset\\object_detection\\HRSC2016_dataset\\resized_images')\n",
    "os.makedirs(new_img_dir,exist_ok=True)\n",
    "img_annotation_dir = Path(r'F:\\dataset\\object_detection\\HRSC2016_dataset\\img_annotation')\n",
    "os.makedirs(img_annotation_dir,exist_ok=True)\n",
    "txt_dir = Path(r'F:\\dataset\\object_detection\\HRSC2016_dataset\\labeltxt')\n",
    "os.makedirs(txt_dir,exist_ok=True)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|██████████| 1055/1055 [01:17<00:00, 13.62it/s]\n"
     ]
    }
   ],
   "source": [
    "for file in tqdm(os.listdir(xml_dir)):\n",
    "    img_path = img_dir/file.replace('.xml','.png')\n",
    "    new_img_path = new_img_dir/file.replace('.xml','.png')\n",
    "    xml_path = xml_dir/file\n",
    "    img_annotation_path = img_annotation_dir/file.replace('.xml','.png')\n",
    "    txt_path = txt_dir/file.replace('.xml','.txt')\n",
    "    xml2annotation_txt(xml_path,img_path,new_img_path,img_annotation_path,txt_path,1024)\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "# 挑选大船数据"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "outputs": [],
   "source": [
    "from pathlib import Path\n",
    "\n",
    "img_chose_dir = Path(r'F:\\dataset\\object_detection\\HRSC2016_dataset\\chosed/images')\n",
    "label_chose_dir = Path(r'F:\\dataset\\object_detection\\HRSC2016_dataset\\chosed/labels')\n",
    "img_remove_dir = Path(r'F:\\dataset\\object_detection\\HRSC2016_dataset\\removed/images')\n",
    "os.makedirs(img_remove_dir,exist_ok=True)\n",
    "label_remove_dir = Path(r'F:\\dataset\\object_detection\\HRSC2016_dataset\\removed/labels')\n",
    "os.makedirs(label_remove_dir,exist_ok=True)\n",
    "img_annotation_dir = Path(r'F:\\dataset\\object_detection\\HRSC2016_dataset\\chosed\\img_annotation')\n",
    "annotation_remove_dir = Path(r'F:\\dataset\\object_detection\\HRSC2016_dataset\\removed/annotaions')\n",
    "os.makedirs(annotation_remove_dir,exist_ok=True)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "outputs": [
    {
     "data": {
      "text/plain": "698"
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import os\n",
    "from tqdm import tqdm\n",
    "\n",
    "list_to_remove = ['100000005','100000008','100000629','100000630','100000632',\n",
    "                  '100000634','100000635','100000636','100000637','100000638',\n",
    "                  '100000640','100000641','100000644','100000646','100000647',\n",
    "                  '100000648','100000650','100000651','100000652','100000653',\n",
    "                  '100000654','100000655','100000657','100000658','100000659',\n",
    "                  '100000660','100000661','100000667','100000668','100000669',\n",
    "                  '100000672','100000673','100000675','100000676','100000681',\n",
    "                  '100000689','100000692','100000693','100000696','100000697',\n",
    "                  '100000704','100000713','100000714','100000716','100000719',\n",
    "                  '100000721','100000722','100000730','100000731','100000744',\n",
    "                  '100000745','100000746','100000753','100000762','100000769',\n",
    "                  '100000770','100000772','100000778','100000784','100000785',\n",
    "                  '100000789','100000791','100000797','100000807','100000809',\n",
    "                  '100000811','100000812','100000823','100000824','100000832',\n",
    "                  '100000833','100000840','100000845','100000846','100000853',\n",
    "                  '100000855','100000857','100000870','100000871','100000873',\n",
    "                  '100000875','100000876','100000877','100000879','100000880',\n",
    "                  '100000883','100000885','100000886','100000887','100000888',\n",
    "                  '100000889','100000890','100000891','100000893','100000894',\n",
    "                  '100000895','100000897','100000898','100000902','100000904',\n",
    "                  '100000907','100000909','100000910','100000911','100000912',\n",
    "                  '100000914','100000915','100000916','100000917','100000920',\n",
    "                  '100000921','100000925','100000926','100000927','100000932',\n",
    "                  '100000933','100000934','100000935','100000936','100000937',\n",
    "                  '100000938','100000939','100000941','100000942','100000943',\n",
    "                  '100000944','100000949','100000950','100000951','100000952',\n",
    "                  '100000953','100000954','100000955','100000956','100000957',\n",
    "                  '100000958','100000959','100000960','100000962','100000963',\n",
    "                  '100000964','100000965','100000966','100000967','100000968',\n",
    "                  '100000969','100000970','100000972','100000973','100000978',\n",
    "                  '100000979','100000980','100000981','100000982','100000983',\n",
    "                  '100000984','100000985','100000986','100000988','100000989',\n",
    "                  '100000990','100000991','100000992','100000995','100000998',\n",
    "                  '100000999','100001001','100001002','100001004','100001005',\n",
    "                  '100001007','100001008','100001009','100001010','100001013',\n",
    "                  '100001014','100001015','100001016','100001017','100001018',\n",
    "                  '100001024','100001025','100001027','100001030','100001032',\n",
    "                  '100001034','100001036','100001037','100001039','100001040',\n",
    "                  '100001042','100001043','100001044','100001045','100001046',\n",
    "                  '100001047','100001048','100001052','100001053','100001054',\n",
    "                  '100001055','100001056','100001057','100001058','100001059',\n",
    "                  '100001060','100001061','100001064','100001065','100001067',\n",
    "                  '100001068','100001069','100001070','100001071','100001072',\n",
    "                  '100001073','100001074','100001075','100001077','100001078',\n",
    "                  '100001079','100001082','100001084','100001085','100001086',\n",
    "                  '100001087','100001088','100001089','100001090','100001091',\n",
    "                  '100001092','100001093','100001094','100001096','100001097',\n",
    "                  '100001098','100001099','100001100','100001102','100001103',\n",
    "                  '100001105','100001106','100001107','100001109','100001110',\n",
    "                  '100001111','100001112','100001113','100001114','100001115',\n",
    "                  '100001117','100001118','100001119','100001120','100001121',\n",
    "                  '100001122','100001123','100001124','100001125','100001126',\n",
    "                  '100001127','100001131','100001132','100001133','100001136',\n",
    "                  '100001138','100001139','100001142','100001144','100001145',\n",
    "                  '100001146','100001147','100001149','100001151','100001152',\n",
    "                  '100001153','100001154','100001155','100001156','100001157',\n",
    "                  '100001158','100001159','100001160','100001161','100001162',\n",
    "                  '100001164','100001165','100001166','100001169','100001171',\n",
    "                  '100001173','100001174','100001175','100001176','100001177',\n",
    "                  '100001178','100001179','100001180','100001181','100001182',\n",
    "                  '100001183','100001185','100001186','100001188','100001189',\n",
    "                  '100001190','100001191','100001192','100001193','100001194',\n",
    "                  '100001195','100001196','100001197','100001198','100001199',\n",
    "                  '100001200','100001201','100001203','100001204','100001205',\n",
    "                  '100001206','100001207','100001208','100001209','100001210',\n",
    "                  '100001211','100001212','100001213','100001214','100001215',\n",
    "                  '100001216','100001217','100001218','100001219','100001220',\n",
    "                  '100001222','100001223','100001224','100001225','100001226',\n",
    "                  '100001227','100001228','100001229','100001231','100001233',\n",
    "                  '100001234','100001235','100001236','100001237','100001238',\n",
    "                  '100001239','100001240','100001241','100001244','100001245',\n",
    "                  '100001246','100001247','100001248','100001249','100001250',\n",
    "                  '100001251','100001252','100001253','100001254','100001255',\n",
    "                  '100001256','100001257','100001258','100001259','100001260',\n",
    "                  '100001263','100001264','100001265','100001266','100001267',\n",
    "                  '100001268','100001269','100001270','100001271','100001272',\n",
    "                  '100001273','100001274','100001276','100001279','100001281',\n",
    "                  '100001282','100001283','100001284','100001285','100001286',\n",
    "                  '100001287','100001288','100001290','100001291','100001295',\n",
    "                  '100001296','100001298','100001299','100001300','100001301',\n",
    "                  '100001302','100001303','100001304','100001306','100001310',\n",
    "                  '100001311','100001312','100001313','100001314','100001315',\n",
    "                  '100001316','100001317','100001318','100001319','100001320',\n",
    "                  '100001321','100001322','100001323','100001324','100001327',\n",
    "                  '100001334','100001335','100001336','100001337','100001338',\n",
    "                  '100001339','100001340','100001341','100001342','100001343',\n",
    "                  '100001344','100001346','100001347','100001351','100001352',\n",
    "                  '100001353','100001354','100001355','100001356','100001357',\n",
    "                  '100001358','100001360','100001361','100001362','100001363',\n",
    "                  '100001364','100001365','100001366','100001367','100001368',\n",
    "                  '100001370','100001371','100001374','100001375','100001376',\n",
    "                  '100001377','100001378','100001379','100001380','100001381',\n",
    "                  '100001382','100001383','100001384','100001385','100001390',\n",
    "                  '100001391','100001392','100001393','100001394','100001395',\n",
    "                  '100001396','100001397','100001398','100001399','100001400',\n",
    "                  '100001401','100001404','100001405','100001407','100001408',\n",
    "                  '100001409','100001410','100001412','100001413','100001414',\n",
    "                  '100001415','100001416','100001418','100001419','100001420',\n",
    "                  '100001421','100001422','100001423','100001424','100001425',\n",
    "                  '100001427','100001428','100001429','100001431','100001432',\n",
    "                  '100001433','100001434','100001435','100001436','100001440',\n",
    "                  '100001441','100001442','100001443','100001444','100001445',\n",
    "                  '100001446','100001447','100001448','100001449','100001451',\n",
    "                  '100001452','100001453','100001454','100001455','100001456',\n",
    "                  '100001457','100001459','100001460','100001461','100001462',\n",
    "                  '100001463','100001464','100001465','100001466','100001467',\n",
    "                  '100001469','100001470','100001472','100001473','100001474',\n",
    "                  '100001475','100001476','100001477','100001478','100001479',\n",
    "                  '100001480','100001481','100001482','100001485','100001486',\n",
    "                  '100001487','100001488','100001489','100001490','100001491',\n",
    "                  '100001492','100001493','100001495','100001496','100001498',\n",
    "                  '100001499','100001500','100001501','100001502','100001503',\n",
    "                  '100001504','100001505','100001507','100001508','100001511',\n",
    "                  '100001512','100001513','100001514','100001515','100001516',\n",
    "                  '100001517','100001518','100001519','100001520','100001521',\n",
    "                  '100001522','100001523','100001524','100001525','100001527',\n",
    "                  '100001528','100001529','100001530','100001531','100001532',\n",
    "                  '100001533','100001534','100001535','100001536','100001540',\n",
    "                  '100001541','100001542','100001543','100001544','100001545',\n",
    "                  '100001546','100001547','100001549','100001552','100001553',\n",
    "                  '100001554','100001555','100001556','100001557','100001558',\n",
    "                  '100001559','100001566','100001567','100001568','100001569',\n",
    "                  '100001570','100001571','100001572','100001573','100001574',\n",
    "                  '100001575','100001576','100001580','100001581','100001582',\n",
    "                  '100001583','100001584','100001585','100001586','100001589',\n",
    "                  '100001591','100001592','100001593','100001594','100001595',\n",
    "                  '100001596','100001597','100001598','100001599','100001601',\n",
    "                  '100001603','100001604','100001605','100001606','100001607',\n",
    "                  '100001608','100001610','100001611','100001612','100001613',\n",
    "                  '100001614','100001615','100001616','100001617','100001618',\n",
    "                  '100001619','100001624','100001625','100001626','100001627',\n",
    "                  '100001628','100001629','100001630','100001631','100001632',\n",
    "                  '100001633','100001634','100001635','100001636','100001637',\n",
    "                  '100001638','100001639','100001640','100001641','100001642',\n",
    "                  '100001643','100001644','100001647','100001649','100001650',\n",
    "                  '100001651','100001652','100001653','100001654','100001655',\n",
    "                  '100001656','100001659','100001661','100001662','100001663',\n",
    "                  '100001664','100001665','100001666','100001668','100001672',\n",
    "                  '100001673','100001674','100001675','100001676','100001677',\n",
    "                  '100001678','100001679','100001680',]\n",
    "len(list_to_remove)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|██████████| 698/698 [00:36<00:00, 19.02it/s]\n"
     ]
    }
   ],
   "source": [
    "import shutil\n",
    "for file in tqdm(list_to_remove):\n",
    "    img_path = img_chose_dir/(file+'.png')\n",
    "    label_path = label_chose_dir/(file+'.txt')\n",
    "    img_annotation_path = img_annotation_dir/(file+'.png')\n",
    "    try:\n",
    "        shutil.copy(img_path,img_remove_dir)\n",
    "        shutil.copy(label_path,label_remove_dir)\n",
    "        shutil.copy(img_annotation_path,annotation_remove_dir)\n",
    "\n",
    "        os.remove(img_path)\n",
    "        os.remove(img_annotation_path)\n",
    "        os.remove(label_path)\n",
    "    except Exception as e:\n",
    "        print(e)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "# 合并港口和大船标签"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "outputs": [],
   "source": [
    "import os\n",
    "img_dir = r'F:\\dataset\\object_detection\\HRSC2016_dataset\\removed\\images'\n",
    "xml_dir = r'F:\\dataset\\object_detection\\HRSC2016_dataset\\removed\\xmls'\n",
    "label_dir = r'F:\\dataset\\object_detection\\HRSC2016_dataset\\removed\\labels'\n",
    "img_anno_dir = r'F:\\dataset\\object_detection\\HRSC2016_dataset\\removed\\img_anno'\n",
    "os.makedirs(img_anno_dir,exist_ok=True)\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|██████████| 731/731 [03:28<00:00,  3.51it/s]\n"
     ]
    }
   ],
   "source": [
    "from tqdm import tqdm\n",
    "\n",
    "for file in tqdm(os.listdir(img_dir)):\n",
    "    img_path = os.path.join(img_dir,file)\n",
    "    xml_path = os.path.join(xml_dir,file.replace('.png','.xml'))\n",
    "    label_path = os.path.join(label_dir,file.replace('.png','.txt'))\n",
    "    img_anno_path = os.path.join(img_anno_dir,file)\n",
    "    merge_xml_txt(xml_path,img_path,img_anno_path,label_path)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "# 处理西安组数据"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 99,
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 97,
   "outputs": [],
   "source": [
    "from pathlib import Path\n",
    "import os\n",
    "imgs_dir = Path(r\"F:\\dataset\\object_detection\\xi'an_data\\L18\\L18\\airport18\\data\")\n",
    "xmls_dir = Path(r\"F:\\dataset\\object_detection\\xi'an_data\\L18\\L18\\airport18\\airport18rolabel\")\n",
    "new_imgs_dir = Path(r\"F:\\dataset\\object_detection\\xi'an_data\\L18\\L18\\airport18\\images\")\n",
    "os.makedirs(new_imgs_dir,exist_ok=True)\n",
    "imgs_annotation_dir = Path(r\"F:\\dataset\\object_detection\\xi'an_data\\L18\\L18\\airport18\\annotations\")\n",
    "os.makedirs(imgs_annotation_dir,exist_ok=True)\n",
    "labeltxts_dir = Path(r\"F:\\dataset\\object_detection\\xi'an_data\\L18\\L18\\airport18\\labels\")\n",
    "os.makedirs(labeltxts_dir,exist_ok=True)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 100,
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|██████████| 62/62 [00:54<00:00,  1.13it/s]\n"
     ]
    }
   ],
   "source": [
    "# 处理整个目录下的文件\n",
    "from tqdm import tqdm\n",
    "\n",
    "for file in tqdm(os.listdir(imgs_dir)):\n",
    "    img_path = imgs_dir/file\n",
    "    xml_path = xmls_dir/file.replace('.tif','.xml')\n",
    "    new_img_path = new_imgs_dir/file\n",
    "    img_annotation_path = imgs_annotation_dir/file\n",
    "    labeltxt_path = labeltxts_dir/file.replace('.tif','.txt')\n",
    "    xml2annotation_txt(xml_path,img_path,new_img_path,img_annotation_path,\n",
    "                       labeltxt_path)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 164,
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|██████████| 1/1 [00:00<00:00,  1.49it/s]\n"
     ]
    }
   ],
   "source": [
    "# 批量处理指定文件\n",
    "from tqdm import tqdm\n",
    "\n",
    "list_imgs = ['US18.tif']\n",
    "for file in tqdm(list_imgs):\n",
    "    img_path = imgs_dir/file\n",
    "    xml_path = xmls_dir/file.replace('.tif','.xml')\n",
    "    new_img_path = new_imgs_dir/file\n",
    "    img_annotation_path = imgs_annotation_dir/file\n",
    "    labeltxt_path = labeltxts_dir/file.replace('.tif','.txt')\n",
    "    xml2annotation_txt(xml_path,img_path,new_img_path,img_annotation_path,\n",
    "                       labeltxt_path)\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "# 将所有txt标签文件里的角度转换为与x轴顺时针的夹角"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "outputs": [],
   "source": [
    "label_dir = r'F:\\dataset\\object_detection\\DOTA-v2.0\\train_data\\bigship\\labels\\train'\n",
    "new_label_dir = r'F:\\dataset\\object_detection\\DOTA-v2.0\\train_data\\bigship\\labels\\train_x'\n",
    "os.makedirs(new_label_dir,exist_ok=True)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|██████████| 1055/1055 [00:01<00:00, 891.92it/s]\n"
     ]
    }
   ],
   "source": [
    "for file in tqdm(os.listdir(label_dir)):\n",
    "    label_path = Path(label_dir)/file\n",
    "    new_label_path = Path(new_label_dir)/file\n",
    "    with open(label_path) as f1, open(new_label_path,'w') as f2:\n",
    "        for line in f1:\n",
    "            data = line.split()\n",
    "            old_angle = int(data[-1])\n",
    "            new_angle = old_angle + 90 if old_angle < 90 else old_angle - 90\n",
    "            data[-1] = str(new_angle)\n",
    "            new_line = ' '.join(data) + '\\n'\n",
    "            f2.write(new_line)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|██████████| 1055/1055 [00:00<00:00, 8330.46it/s]\n"
     ]
    }
   ],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "import os\n",
    "from tqdm import tqdm\n",
    "\n",
    "label_txt_dir = r'/media/xiashuobad/000FF0B80007C568/dataset/object_detection/DOTA-v2.0/train_data/bigship/labels/train_x'\n",
    "list_h,list_w,list_thr = [],[],[]\n",
    "for file in tqdm(os.listdir(label_txt_dir)):\n",
    "    file_path = os.path.join(label_txt_dir,file)\n",
    "    with open(file_path) as f:\n",
    "        for line in f:\n",
    "            data = line.split()\n",
    "            h = float(data[3])\n",
    "            w = float(data[4])\n",
    "            list_h.append(h)\n",
    "            list_w.append(w)\n",
    "            list_thr.append(h//w)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "outputs": [
    {
     "data": {
      "text/plain": "<Figure size 432x288 with 1 Axes>",
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAYAAAAD4CAYAAADlwTGnAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjMuNCwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8QVMy6AAAACXBIWXMAAAsTAAALEwEAmpwYAAAwEUlEQVR4nO2dfZRU5Zngf08XhVabSKN29mgBQhIGVkKkA1FmPZsZzETIGLGPkqDRie7JiSeT457VZPpMZ+IIOmYkw0nMzqwnEyffE6MYSHpwMEuykezscYKHJt1I2sgEv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zCMNqVhdwCGYRhGdTEBYBiG0aaYADAMw2hTTAAYhmG0KSYADMMw2hQTAIZhGG2KCQDDMIw25f8D09lLn6n9WrsAAAAASUVORK5CYII=\n"
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.scatter(list_h,list_w)\n",
    "plt.show()"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "outputs": [
    {
     "data": {
      "text/plain": "<Figure size 432x288 with 1 Axes>",
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXAAAAD4CAYAAAD1jb0+AAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjMuNCwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8QVMy6AAAACXBIWXMAAAsTAAALEwEAmpwYAAAi3klEQVR4nO3df5Ac5Zkf8O8zvb2r2UXW7JqVIi1aCzYqUZyFJLw5iazLwXawfNgOa4wAHao4vhRUfudCnS4ouGKnCpd8twfxXVXqrvDZOTtWydhAdFRyF5nEuJwQa8nKAgSGNRK/xILRnqXFoF2j1erJH9M96unt7unpeWem39X3U6XSbM/M+6vffnZ2uvt5RVVBRET2KbS7AURElA0DOBGRpRjAiYgsxQBORGQpBnAiIkt1tLKySy+9VNetW9fKKomIrHf48OG/UdX+8PaWBvB169ZhYmKilVUSEVlPRF6N2s6vUIiILMUATkRkKQZwIiJLMYATEVmKAZyIyFItvQqFCAAOHJnC2MFJvDEzhzWlInZv34DRLQNtK4fIVgzg1FIHjkxhzyNHMTe/AACYmpnDnkeOAkBdwddUOUQ241co1FJjBycrQdc3N7+AsYOTbSmHyGYM4NRSb8zM1bW92eUQ2YwBnFpqTalY1/Zml0NkMwZwaqnd2zeg6DpV24qug93bN7SlHCKb8SQmtZR/grHRq0dMlUNkM2nlmpjDw8PKZFZERPURkcOqOhzezq9QiIgsxQBORGQpBnAiIksxgB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en1/gz+hLet7ULnFz6L8r4Byn25zbtK4HIA6wE8iZzMQe970m8AeF5V7w88Zd2+ieuLjftGRPpFpOQ9LgK4HuXv9B8HcLP3svB+8ffXzQB+6P3lFNfHeK08W5v1H8pn03+O8vdK97S7PSnaewXKZ5OfBvCc32aUv+f6XwBeBPA/AfTphbPY/8nr31EAw21u/36U/3ydR/l7uH+cpe0AfgflEzHHAHwhR335L15bn/EOmtWB19/j9WUSwG/laQ4C+DDKX488A+Ap798NNu6bhL5Yt28AXA3giNfmZwH8e2/7FSgH4GMAvg+gy9u+zPv5mPf8FbX6GPePt9ITEVnKhq9QiIgoAgM4EZGlGMCJiCzFAE5EZCkGcCIiSzGAExFZigGciMhS/x9inKRAhvucDwAAAABJRU5ErkJggg==\n"
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "import numpy as np\n",
    "n = len(list_thr)\n",
    "x = np.array(range(n))\n",
    "y = np.array(list_thr)\n",
    "plt.scatter(x,y,marker='o')\n",
    "plt.show()"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 处理oiltank油罐数据"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 转换yolov5正框标签格式"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "outputs": [],
   "source": [
    "def batch_transfer_labels(image_dir,old_label_dir,new_label_dir,annotation_dir):\n",
    "    \"\"\"\n",
    "    批量转换原始标签格式为yolov5要求的标签格式，并根据标签对图片进行标签可视化并保存\n",
    "    原始标签：oiltank_1.jpg oiltank xmin ymin xmax ymax\n",
    "    yolov5标签：0 center_x center_y box_w box_h\n",
    "    :param image_dir:\n",
    "    :param old_label_dir:\n",
    "    :param new_label_dir:\n",
    "    :param annotation_dir:\n",
    "    :return:\n",
    "    \"\"\"\n",
    "    os.makedirs(new_label_dir,exist_ok=True)\n",
    "    os.makedirs(annotation_dir,exist_ok=True)\n",
    "    for file in tqdm(os.listdir(old_label_dir)):\n",
    "        image_path = os.path.join(image_dir,file.replace('.txt','.jpg'))\n",
    "        image = Image.open(image_path)\n",
    "        draw = ImageDraw.Draw(image)\n",
    "        w,h = image.size\n",
    "        old_label_path = os.path.join(old_label_dir,file)\n",
    "        new_label_path = os.path.join(new_label_dir,file)\n",
    "        with open(old_label_path) as f1,open(new_label_path,'w') as f2:\n",
    "            for line in f1:\n",
    "                box = line.strip().split()\n",
    "                xmin,ymin,xmax,ymax = map(float,box[-4:])\n",
    "                xywh,points = get_xywh_points(xmin,ymin,xmax,ymax)\n",
    "                center_x,center_y,box_w,box_h = xywh\n",
    "                draw.polygon(points,outline='red')\n",
    "                f2.write(f'0 {center_x/w} {center_y/h} {box_w/w} {box_h/h}\\n')\n",
    "            annotation_path = os.path.join(annotation_dir,file.replace('.txt','.jpg'))\n",
    "            image.save(annotation_path)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "outputs": [],
   "source": [
    "def get_xywh_points(xmin,ymin,xmax,ymax):\n",
    "    \"\"\"\n",
    "    根据xmin,ymin,xmax,ymax求center_x,center_y,w,h\n",
    "    和四个顶点坐标\n",
    "    :param xmin:\n",
    "    :param ymin:\n",
    "    :param xmax:\n",
    "    :param ymax:\n",
    "    :return:\n",
    "    \"\"\"\n",
    "    center_x,center_y = (xmin+xmax)/2, (ymin+ymax)/2\n",
    "    box_w,box_h = xmax -xmin,ymax-ymin\n",
    "    x0,y0 = xmin,ymin # 左上角\n",
    "    x1,y1 = x0+box_w, y0 # 右上角\n",
    "    x2,y2 = x1,y0+box_h # 右下角\n",
    "    x3,y3 = x0,y2 # 左下角\n",
    "    return [center_x,center_y,box_w,box_h],[x0,y0,x1,y1,x2,y2,x3,y3]"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|██████████| 165/165 [00:05<00:00, 28.90it/s]\n"
     ]
    }
   ],
   "source": [
    "batch_transfer_labels(r'F:\\目标检测\\油罐\\oiltank\\images',\n",
    "                      r'F:\\目标检测\\油罐\\oiltank\\Annotation\\old_labels',\n",
    "                      r'F:\\目标检测\\油罐\\oiltank\\labels',\n",
    "                      r'F:\\目标检测\\油罐\\oiltank\\annotations')\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 从LEVIR数据集中单独提取油罐数据"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|██████████| 43904/43904 [01:34<00:00, 464.61it/s] \n"
     ]
    }
   ],
   "source": [
    "\n",
    "data_dir = r'F:\\目标检测\\LEVIR\\imageWithLabel'\n",
    "label_dir = r'F:\\目标检测\\LEVIR\\油罐/labels'\n",
    "annotation_dir = r'F:\\目标检测\\LEVIR\\油罐/annotations'\n",
    "img_dir = r'F:\\目标检测\\LEVIR\\油罐/images'\n",
    "os.makedirs(label_dir,exist_ok=True)\n",
    "os.makedirs(annotation_dir,exist_ok=True)\n",
    "os.makedirs(img_dir,exist_ok=True)\n",
    "\n",
    "for file in tqdm(os.listdir(data_dir)):\n",
    "    if file.endswith('.txt'):\n",
    "        file_path = Path(data_dir) / file\n",
    "        label_path = Path(label_dir) / file\n",
    "        with open(file_path) as f1,open(label_path,'w') as f2:\n",
    "             data = f1.readlines()\n",
    "             if data:\n",
    "                 img_path = Path(data_dir) / file.replace('.txt','.jpg')\n",
    "                 img = Image.open(img_path)\n",
    "                 w,h = img.size\n",
    "                 draw = ImageDraw.Draw(img)\n",
    "                 for line in data:\n",
    "                     box = line.strip().split()\n",
    "                     if box[0] == '3':\n",
    "                         id,xmin,ymin,xmax,ymax = [int(val) for val in box]\n",
    "                         xywh,points = get_xywh_points(xmin,ymin,xmax,ymax)\n",
    "                         draw.polygon(points,outline='red')\n",
    "                         center_x,center_y,box_w,box_h = xywh\n",
    "                         f2.write(f'0 {center_x/w} {center_y/h} {box_w/w} {box_h/h}\\n')\n",
    "\n",
    "             offset = f2.tell()\n",
    "\n",
    "        if offset > 0:\n",
    "            annotation_path = Path(annotation_dir) / file.replace('.txt','.jpg')\n",
    "            img.save(annotation_path)\n",
    "            shutil.copy(img_path,img_dir)\n",
    "        else:\n",
    "            os.remove(label_path)\n",
    "\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 从NWPU VHR-10数据中提取油罐数据\n",
    "\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|██████████| 650/650 [00:17<00:00, 37.29it/s]\n"
     ]
    }
   ],
   "source": [
    "import re\n",
    "\n",
    "old_label_dir = r'F:\\目标检测\\NWPU VHR-10 dataset\\ground truth'\n",
    "p_img_dir = r'F:\\目标检测\\NWPU VHR-10 dataset\\positive image set'\n",
    "new_label_dir = r'F:\\目标检测\\NWPU VHR-10 dataset\\油罐/labels'\n",
    "img_dir = r'F:\\目标检测\\NWPU VHR-10 dataset\\油罐/images'\n",
    "annotation_dir = r'F:\\目标检测\\NWPU VHR-10 dataset\\油罐/annotations'\n",
    "os.makedirs(new_label_dir,exist_ok=True)\n",
    "os.makedirs(img_dir,exist_ok=True)\n",
    "os.makedirs(annotation_dir,exist_ok=True)\n",
    "\n",
    "for file in tqdm(os.listdir(old_label_dir)):\n",
    "    file_path = Path(old_label_dir) / file\n",
    "    label_path = Path(new_label_dir) /file\n",
    "    with open(file_path) as f1, open(label_path,'w') as f2:\n",
    "        data = f1.readlines()\n",
    "        if data:\n",
    "             img_path = Path(p_img_dir) / file.replace('.txt','.jpg')\n",
    "             img = Image.open(img_path)\n",
    "             w,h = img.size\n",
    "             draw = ImageDraw.Draw(img)\n",
    "             for line in data:\n",
    "                 box = re.findall('\\((.*),(.*)\\),\\((.*),(.*)\\),(.*)',line)\n",
    "                 if not box:\n",
    "                     continue\n",
    "                 xmin,ymin,xmax,ymax,id = map(int,box[0])\n",
    "                 if id == 3:\n",
    "                     xywh,points = get_xywh_points(xmin,ymin,xmax,ymax)\n",
    "                     draw.polygon(points,outline='red')\n",
    "                     center_x,center_y,box_w,box_h = xywh\n",
    "                     f2.write(f'0 {center_x/w} {center_y/h} {box_w/w} {box_h/h}\\n')\n",
    "\n",
    "        offset = f2.tell()\n",
    "\n",
    "    if offset > 0:\n",
    "        annotation_path = Path(annotation_dir) / file.replace('.txt','.jpg')\n",
    "        img.save(annotation_path)\n",
    "        shutil.copy(img_path,img_dir)\n",
    "    else:\n",
    "        os.remove(label_path)\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 从HRRSD数据集中提取油罐数据\n",
    "\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "  5%|▌         | 1166/21761 [00:31<05:47, 59.26it/s]D:\\programming\\anaconda3\\envs\\yolov5-obb\\lib\\site-packages\\PIL\\Image.py:2855: DecompressionBombWarning: Image size (94434015 pixels) exceeds limit of 89478485 pixels, could be decompression bomb DOS attack.\n",
      "  warnings.warn(\n",
      "100%|██████████| 21761/21761 [09:00<00:00, 40.30it/s] \n"
     ]
    }
   ],
   "source": [
    "img_all_dir = r'F:\\目标检测\\HRRSD\\images_all'\n",
    "xml_dir = r'F:\\目标检测\\HRRSD\\TGRS-HRRSD-Dataset-mstr-gthb\\OPT2017\\Annotations'\n",
    "label_dir = r'F:\\目标检测\\HRRSD\\油罐/labels'\n",
    "img_dir = r'F:\\目标检测\\HRRSD\\油罐/images'\n",
    "annotation_dir = r'F:\\目标检测\\HRRSD\\油罐/annotations'\n",
    "os.makedirs(label_dir,exist_ok=True)\n",
    "os.makedirs(img_dir,exist_ok=True)\n",
    "os.makedirs(annotation_dir,exist_ok=True)\n",
    "\n",
    "for xml in tqdm(os.listdir(xml_dir)):\n",
    "    xml_path = Path(xml_dir) / xml\n",
    "    label_path = Path(label_dir) / xml.replace('.xml','.txt')\n",
    "    img_path = Path(img_all_dir) / xml.replace('.xml','.jpg')\n",
    "    dom = parse(str(xml_path))\n",
    "    root = dom.documentElement\n",
    "    objects = root.getElementsByTagName(\"object\")\n",
    "    f = open(label_path, 'w')\n",
    "    img = Image.open(img_path)\n",
    "    w,h = img.size\n",
    "    draw = ImageDraw.Draw(img)\n",
    "    for box in objects:\n",
    "        cls_name = box.getElementsByTagName(\"name\")[0].childNodes[0].data\n",
    "        if 'tank' in cls_name:\n",
    "            xmin = float(box.getElementsByTagName(\"xmin\")[0].childNodes[0].data)\n",
    "            ymin = float(box.getElementsByTagName(\"ymin\")[0].childNodes[0].data)\n",
    "            xmax = float(box.getElementsByTagName(\"xmax\")[0].childNodes[0].data)\n",
    "            ymax = float(box.getElementsByTagName(\"ymax\")[0].childNodes[0].data)\n",
    "            xywh,points = get_xywh_points(xmin,ymin,xmax,ymax)\n",
    "            draw.polygon(points,outline='red')\n",
    "            center_x,center_y,box_w,box_h = xywh\n",
    "            f.write(f'0 {center_x/w} {center_y/h} {box_w/w} {box_h/h}\\n')\n",
    "    offset = f.tell()\n",
    "    f.close()\n",
    "    if offset > 0:\n",
    "        annotation_path = Path(annotation_dir) / xml.replace('.xml','.jpg')\n",
    "        img.save(annotation_path)\n",
    "        shutil.copy(img_path,img_dir)\n",
    "    else:\n",
    "        os.remove(label_path)\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "    label information of 5 classes:\n",
    "    built-up\n",
    "    RGB:    255,    0,    0\n",
    "    farmland\n",
    "    RGB:    0,    255,    0\n",
    "    forest\n",
    "    RGB:    0,    255,  255\n",
    "    meadow\n",
    "    RGB:    255,  255,   0\n",
    "    water\n",
    "    RGB:    0,     0,  255\n"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "outputs": [
    {
     "data": {
      "text/plain": "array([[0, 0],\n       [1, 1]])"
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "a = np.array([[[255,0,0],[0,255,0]],[[0,255,255],[255,255,0]]])\n",
    "label = np.where(((a[:,:,0] == 0) & (a[:,:,1] == 255) & (a[:,:,2] == 255)) |\n",
    "                 ((a[:,:,0] == 255) & (a[:,:,1] == 255) & (a[:,:,2] == 0)),1,0)\n",
    "label\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "name": "python3",
   "language": "python",
   "display_name": "Python 3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}